package com.woniuxy.lrz.carlproject.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.util.BeanCopyUtil;
import com.woniuxy.lrz.carlproject.common.util.Results;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsCategoryAddOrUpdateForm;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsCategoryFindForm;
import com.woniuxy.lrz.carlproject.model.GoodCategory;
import com.woniuxy.lrz.carlproject.service.GoodCategoryService;
import com.woniuxy.lrz.carlproject.service.dto.dtogoods.GoodsCategoryDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 商品分类表 前端控制器
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@RestController
@Api(tags = "商品分类接口")
@Validated
@RequestMapping("/carlproject/goodCategory")
public class GoodCategoryController {
    @Resource
    private GoodCategoryService goodCategoryService;

    @PostMapping("/addGoodCategory")
    @ApiOperation("新增/修改商品分类")
    public Results addGoodCategory(@RequestBody @Valid GoodsCategoryAddOrUpdateForm goodsCategoryAddOrUpdateForms){
        goodCategoryService.addGoodCategory(goodsCategoryAddOrUpdateForms);
        return Results.instance().setMessage("操作成功");
    }

    @DeleteMapping("/deleteGoodCategory")
    @ApiOperation("删除商品分类")
    @ApiImplicitParam(name = "goodCategoryId",value = "商品分类id")
    public Results deleteGoodCategory(@NotNull(message = "商品分类Id不能为空") Integer goodCategoryId){
        goodCategoryService.deleteGoodCategory(goodCategoryId);
        return Results.instance().setMessage("删除成功");
    }

    @GetMapping("/findGoodCateGory")
    @ApiOperation("加载商品分类信息")
    public Results findGoodCateGory(@Valid GoodsCategoryFindForm goodsCategoryFindForm){
        Page<GoodsCategoryDto> goodCateGory = goodCategoryService.findGoodCateGory(goodsCategoryFindForm);
        return Results.instance().setMessage("加载商品分类信息成功").setData(goodCateGory);
    }

    @GetMapping("/findGoodCategoryCondition")
    @ApiOperation("加载商品分类下拉列表")
    @Cacheable(value = "goods",key = "'goodsCategory:'")
    public Results findGoodCategoryCondition(){
        List<GoodCategory> list = goodCategoryService.list();
        List<GoodsCategoryDto> goodsCategoryDtos = BeanUtil.copyToList(list, GoodsCategoryDto.class);
        return Results.instance().setData(goodsCategoryDtos).setMessage("加载商品分类下拉列表成功");
    }

}
